package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.EmsProtectSoeEventRecord;
import com.econ.powercloud.ems.dal.mapper.EmsProtectSoeEventRecordMapper;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author : zjf
 * @Description :
 * @Date : 2022/8/3 14:43
 * @Modified by : zjf
 */
@Repository
public class EmsProtectSoeEventRecordDAO extends ServiceImpl<EmsProtectSoeEventRecordMapper, EmsProtectSoeEventRecord> {

    public EmsProtectSoeEventRecord selectOneByCondition(Integer eventType, String deviceCode, Integer actionType,
                                                         Integer soeEvent,
                                                         Long gmtCreate) {
        LambdaQueryWrapper<EmsProtectSoeEventRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(eventType != null, EmsProtectSoeEventRecord::getEventType, eventType);
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(deviceCode), EmsProtectSoeEventRecord::getDeviceCode, deviceCode);
        lambdaQueryWrapper.eq(actionType != null, EmsProtectSoeEventRecord::getActionType, actionType);
        lambdaQueryWrapper.eq(soeEvent != null, EmsProtectSoeEventRecord::getSoeEvent, soeEvent);
        lambdaQueryWrapper.eq(gmtCreate != null, EmsProtectSoeEventRecord::getGmtCreate, gmtCreate);
        lambdaQueryWrapper.last("limit 1");
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    public Page<EmsProtectSoeEventRecord> selectListByCondition(Integer eventType, List<String> deviceCodeList, Integer phaseType,
                                                                Integer actionType, String soeEventName,
                                                                Integer soeStatus, Long startTime, Long endTime,
                                                                Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<EmsProtectSoeEventRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(eventType != null, EmsProtectSoeEventRecord::getEventType, eventType);
        wrapper.in(CollectionUtils.isNotEmpty(deviceCodeList), EmsProtectSoeEventRecord::getDeviceCode, deviceCodeList);
        wrapper.eq(phaseType != null, EmsProtectSoeEventRecord::getPhaseType, phaseType);
        wrapper.eq(actionType != null, EmsProtectSoeEventRecord::getActionType, actionType);
        wrapper.like(StringUtils.isNotBlank(soeEventName), EmsProtectSoeEventRecord::getSoeEventName, soeEventName);
        wrapper.eq(soeStatus != null, EmsProtectSoeEventRecord::getSoeStatus, soeStatus);
        wrapper.ge(startTime != null, EmsProtectSoeEventRecord::getGmtCreate, startTime);
        wrapper.le(endTime != null, EmsProtectSoeEventRecord::getGmtCreate, endTime);
        Page<EmsProtectSoeEventRecord> page = new Page<>(pageNo, pageSize);
        return this.baseMapper.selectPage(page, wrapper);
    }

    /**
     * 按照条件,获取设备的最新一条记录
     */
    public Map<String,EmsProtectSoeEventRecord> selectLeastRecordWithDeviceByCondition(Integer eventType){
        List<EmsProtectSoeEventRecord> eventRecords = this.baseMapper.selectLeastRecordWithDeviceByCondition(eventType);
        if(CollectionUtils.isEmpty(eventRecords)){
            return Collections.emptyMap();
        }
        Map<String,EmsProtectSoeEventRecord> result = Maps.newHashMap();
        Map<String,List<EmsProtectSoeEventRecord>> eventMap = eventRecords.stream().collect(Collectors.groupingBy(d->d.getDeviceCode()));
        eventMap.forEach((k,v)->{
            EmsProtectSoeEventRecord record = v.stream().max(Comparator.comparing(p -> p.getGmtCreate())).get();
            result.put(k,record);
        });
        return result;
    }

}

